package com.bc004346.travelus.Adapter;

import android.app.Activity;
import android.graphics.Typeface;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import com.bc004346.travelus.Model.Domain.Flight;
import com.bc004346.travelus.R;

import java.util.ArrayList;

import static com.bc004346.travelus.Utility.Utility.DAY_DATE;
import static com.bc004346.travelus.Utility.Utility.MILITARY_TIME;

/**
 * Created by Renats on 9/5/2014.
 * Generic * Flight adapter
 */
public class FlightAdapter extends ArrayAdapter<Flight> {

    private Activity mActivity;

    public FlightAdapter(Activity activity, ArrayList<Flight> flights) {
        super(activity, 0, flights);
        mActivity = activity;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null)
            convertView = mActivity.getLayoutInflater()
                    .inflate(R.layout.list_item_flight, null);

        Flight f = getItem(position);
        Typeface font = Typeface.createFromAsset(mActivity.getAssets(), "arial.ttf");

        TextView flightNumber = (TextView) convertView.findViewById(R.id.list_item_flight_flight_number);
        TextView departureDate = (TextView) convertView.findViewById(R.id.list_item_flight_departure_date);
        TextView originDestination = (TextView) convertView.findViewById(R.id.list_item_flight_origin_destination);

        flightNumber.setText(String.format("Flight %s", String.valueOf(f.getFlightNumber())));
        flightNumber.setTypeface(font);

        departureDate.setText(String.format("%s %s",
                MILITARY_TIME.format(f.getDeparture()),
                DAY_DATE.format(f.getDeparture())));
        departureDate.setTypeface(font);

        originDestination.setText(String.format("%s > %s",
                f.getOrigin(),
                f.getDestination()));
        originDestination.setTypeface(font);

        return convertView;
    }
}
